﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using FunctionalCLR.Shell;

namespace Zove.Desktop.Tools {
    public partial class InteractiveMode : Form, IDesktopForm {

        private Interpreter intp = null;

        public InteractiveMode() {
            InitializeComponent();

            TextBoxWriter tbw = new TextBoxWriter(this.tecOutput);
            this.intp = new Interpreter(new ShellContext());
        }

        private void btnCommit_Click(object sender, EventArgs e) {
            this.Execute();
        }

        #region IDesktopForm Members

        public void Execute() {
            string output = this.intp.Process(this.tecInput.Text) as string;
            this.tecOutput.Text = this.tecOutput.Text + output + "\n";
            //this.tecInput.ActiveTextAreaControl.TextArea.SelectionManager.SetSelection(new TextLocation(0, 0), new TextLocation(1, 1));
        }

        public DigitalRune.Windows.TextEditor.TextEditorControl GetTextEditorControl() {
            return this.tecInput;
        }

        public string GetFileName() {
            throw new NotImplementedException();
        }

        public string GetFileExtension() {
            throw new NotImplementedException();
        }

        public void Save(string fileName) {
            StringBuilder b = new StringBuilder();

            foreach (string stmt in ((ShellContext)this.intp.Context).InputHistory) {
                b.Append(stmt);
            }

            File.WriteAllText(fileName, b.ToString());
        }

        public string GetFileFilter() {
            throw new NotImplementedException();
        }

        public string GetColouringScheme() {
            return "Zoom";
        }

        #endregion
    }
}
